package com.viewpagerindicator.sample;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.viewpagerindicator.TabPageIndicator;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {
  private ViewPager pager;
  private TabPageIndicator indicator;
    //分类标题、装载Fragment的集合
    private String[] names = {"全部", "人造革", "真皮", "超纤"};
    private List<Fragment> contentFragment = new ArrayList<Fragment>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pager = (ViewPager) findViewById(R.id.pager);
        indicator = (TabPageIndicator) findViewById(R.id.indicator);


        //创建Fragment
        GenuineLeatherFragment genuineLeatherFragment = new GenuineLeatherFragment();
        GenuineLeatherFragment genuineLeatherFragment2 = new GenuineLeatherFragment();
        GenuineLeatherFragment genuineLeatherFragment3 = new GenuineLeatherFragment();
        GenuineLeatherFragment genuineLeatherFragment4 = new GenuineLeatherFragment();
        contentFragment.add(genuineLeatherFragment);
        contentFragment.add(genuineLeatherFragment2);
        contentFragment.add(genuineLeatherFragment3);
        contentFragment.add(genuineLeatherFragment4);


        //为viewpager设置适配器，一定要注意是FragmentPagerAdapter
        pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            //返回对应位置上的Fragment
            @Override
            public Fragment getItem(int position) {
                return contentFragment.get(position);
            }
            //获取Fragment的数量
            @Override
            public int getCount() {
                return contentFragment.size();
            }
            //为对应的Fragment设置标题
            @Override
            public CharSequence getPageTitle(int position) {
                return names[position];
            }
        });

        indicator.setViewPager(pager);//关联

    }
}
